package com.yunwaikeji.gy_tool_library.set;

import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yunwaikeji.gy_tool_library.interface1.GYViewGroupSetInterface;
import com.yunwaikeji.gy_tool_library.utils.GYViewUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class GYViewGroupSetSet<V extends View, VS extends GYViewSetSet<V, VS>> extends GYViewSetSet<V, VS> implements GYViewGroupSetInterface<VS> {
    private ViewGroup viewGroup;

    public GYViewGroupSetSet(View view) {
        super(view);
        init(view);
    }

    private void init(View view) {
        if (view instanceof ViewGroup) {
            viewGroup = (ViewGroup) view;
        }
    }

    @Override
    public VS setGravity(int gravity) {
        if (view instanceof LinearLayout) {
            LinearLayout linearLayout = (LinearLayout) view;
            linearLayout.setGravity(gravity);
        } else if (view instanceof RelativeLayout) {
            RelativeLayout relativeLayout = (RelativeLayout) view;
            relativeLayout.setGravity(gravity);
        } else if (view instanceof TextView) {
            TextView textView = (TextView) view;
            textView.setGravity(gravity);
        }
        return vs;
    }

    @Override
    public VS setViews(View... views) {
        return setViews(Arrays.asList(views));
    }

    @Override
    public VS setViews(List<View> viewList) {
        if (viewGroup != null) {
            viewGroup.removeAllViews();
        }
        return addViews(viewList);
    }

    @Override
    public VS addViews(View... views) {
        return addViews(Arrays.asList(views));
    }

    @Override
    public VS addViews(int index, View... views) {
        return addViews(index, Arrays.asList(views));
    }

    @Override
    public VS addViews(List<View> viewList) {
        return addViews(-1, viewList);
    }

    @Override
    public VS addViews(int index, List<View> viewList) {
        if (viewGroup != null) {
            GYViewUtils.addViews().addViews(viewGroup, index, viewList);
        }
        return vs;
    }

    @Override
    public List<View> getChildViews() {
        if (viewGroup != null) {
            return GYViewUtils.getChildViewList(viewGroup);
        }
        return new ArrayList<>();
    }

    @Override
    public List<View> getViews() {
        List<View> viewList = new ArrayList<>();
        if (viewGroup != null) {
            for (int i = 0; i < viewGroup.getChildCount(); i++) {
                viewList.add(viewGroup.getChildAt(i));
            }
        }
        return viewList;
    }

    @Override
    public VS removeAllViews() {
        if (viewGroup != null) {
            viewGroup.removeAllViews();
        }
        return vs;
    }

    @Override
    public VS removeViewAt(int index) {
        if (viewGroup != null) {
            viewGroup.removeViewAt(index);
        }
        return vs;
    }

    @Override
    public VS removeView(View view) {
        if (viewGroup != null) {
            viewGroup.removeView(view);
        }
        return vs;
    }

    @Override
    public VS removeAllViewsInLayout() {
        if (viewGroup != null) {
            viewGroup.removeAllViewsInLayout();
        }
        return vs;
    }

    @Override
    public VS removeViewInLayout(View view) {
        if (viewGroup != null) {
            viewGroup.removeViewInLayout(view);
        }
        return vs;
    }

    @Override
    public VS removeViewsInLayout(int start, int count) {
        if (viewGroup != null) {
            viewGroup.removeViewsInLayout(start, count);
        }
        return vs;
    }

}
